package com.guli.generator.service.impl;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.generator.listener.SubjectListener;
import com.guli.generator.mapper.EduSubjectMapper;
import com.guli.generator.pojo.EduSubject;
import com.guli.generator.service.EduSubjectService;

import com.guli.generator.subject.OneSubject;
import com.guli.generator.subject.TowSubject;
import com.guli.generator.ulits.Subject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【edu_subject(课程科目)】的数据库操作Service实现
* @createDate 2021-12-28 20:25:55
*/
@Service
public class EduSubjectServiceImpl implements EduSubjectService {
    @Resource
    private EduSubjectMapper subjectMapper;

    @Override
    public void saveSubject(MultipartFile file,EduSubjectMapper subjectMapper) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, Subject.class,new SubjectListener(subjectMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getEduSubject() {
        //查询一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper();
        wrapper.eq("parent_id","0");
        List<EduSubject> noeSub = subjectMapper.selectList(wrapper);
        //查询二级分类
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper.ne("parent_id",0);
        List<EduSubject> towSub = subjectMapper.selectList(wrapper1);

        List<OneSubject> oneSubjects = new ArrayList<>();

        OneSubject oneSubject = null;

        for (EduSubject eduSubject : noeSub) {
            oneSubject = new OneSubject();
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            oneSubjects.add(oneSubject);
            List<TowSubject> towSubjectList = new ArrayList<>();
            for (EduSubject towSubjects : towSub) {
                if(towSubjects.getParentId().equals(eduSubject.getId())){
                    TowSubject towSubject = new TowSubject();
                    towSubject.setId(towSubjects.getId());
                    towSubject.setTitle(towSubjects.getTitle());
                    towSubjectList.add(towSubject);
                    oneSubject.setChildren(towSubjectList);
                }

            }
        }
        return oneSubjects;
    }
}
